/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trim.gui.testing.models;

import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import trim.common.Material;
import trim.common.inputdata.MaterialStock;
import trim.common.inputdata.InputData;

/**
 *
 * @author Дмитрий
 */
public class RequestMaterialsModel extends AbstractTableModel
        implements IRowManipulationTableModel,TableModelListener
{

  List<MaterialStock> materialCountList;
  boolean editable;

  public RequestMaterialsModel(InputData inputData)
  {
    this.materialCountList=inputData.getMaterialStockList();
  }

  public RequestMaterialsModel(List<MaterialStock> materialCountList,boolean editable)
  {
    this.materialCountList=materialCountList;
    this.editable=editable;
  }

  @Override
  public String getColumnName(int column)
  {
    switch(column){
      case 0:
        return "Material";
      case 1:
        return "Count";
    }
    return super.getColumnName(column);
  }

  @Override
  public Class<?> getColumnClass(int columnIndex)
  {
    switch(columnIndex)
    {
      case 0:return Integer.class;
      case 1:return Integer.class;
    }
    return String.class;
  }


  public int getRowCount()
  {
    return materialCountList.size();
  }

  public int getColumnCount()
  {
    return 2;
  }

  public Object getValueAt(int rowIndex,int columnIndex)
  {
    switch(columnIndex){
      case 0:
        return materialCountList.get(rowIndex).getMaterial().getLength();
      case 1:
        return materialCountList.get(rowIndex).getCount();
    }
    return 0;
  }

  @Override
  public boolean isCellEditable(int row,int col)
  {
    return col==1;
  }

  @Override
  public void setValueAt(Object aValue,int rowIndex,int columnIndex)
  {
    switch(columnIndex){
      //case 0:getFormats().set(rowIndex,Integer.parseInt(aValue.toString()));break;
      case 1:
        materialCountList.get(rowIndex).setCount(Integer.parseInt(aValue.toString()));
        break;
    }
    //fireTableDataChanged();
    fireTableRowsUpdated(rowIndex,rowIndex);
  }

  public void addRow()
  {
    materialCountList.add(new MaterialStock(new Material(0),0));
    fireTableRowsInserted(materialCountList.size()-1,materialCountList.size()-1);
  }

  public void removeRow(int i)
  {
    materialCountList.remove(i);
    fireTableRowsDeleted(i,i);
  }

  public void insertRow(int r)
  {
    materialCountList.add(r,new MaterialStock(new Material(0),0));
    fireTableRowsInserted(r,r);
  }

  public void tableChanged(TableModelEvent e)
  {
    fireTableDataChanged();
  }

  public Object getObject(int r)
  {
    return materialCountList.get(r).getMaterial();
  }
}
